﻿using System;
using System.Collections.Generic;
using System.CodeDom;
using BMC.ARSystem;

namespace Wicresoft.EKA.NRemedy.CodeGenerate
{
    public class GenerateStatusHistoryProperty : IGenerateProperty
    {
        public void Create(CodeTypeDeclaration classType, string propertyName,
            string propertyType, Limit limit, Dictionary<string, object> attributePropertyValues)
        {
            if (classType == null) throw new ArgumentNullException("classType");
            if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");
            if (string.IsNullOrEmpty(propertyType)) throw new ArgumentNullException("propertyType");
            if (attributePropertyValues == null) throw new ArgumentNullException("attributePropertyValues");

            GenerateNameResolver gnr = new GenerateNameResolver();
            CodeMemberProperty _property = new CodeMemberProperty();
            _property.Name = propertyName;
            _property.Type = new CodeTypeReference(propertyType);

            _property.Attributes = MemberAttributes.Public | MemberAttributes.Final;


            _property.GetStatements.Add(new CodeVariableDeclarationStatement(typeof(object), "temp", new CodeIndexerExpression(new CodeFieldReferenceExpression(
                new CodeThisReferenceExpression(), "ValueList"),
                new CodePrimitiveExpression(attributePropertyValues["DatabaseID"]))));

            //如果想做成Nullable的 可以直接将return default(T)换为NULL，OR直接用NUllAble 强制转换。 
            _property.GetStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("temp"),
                CodeBinaryOperatorType.ValueEquality,
               new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.DBNull"), "Value")), CodeBinaryOperatorType.BooleanOr,
               new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("temp"), CodeBinaryOperatorType.ValueEquality,
               new CodePrimitiveExpression(null))),
               new CodeStatement[] { new CodeMethodReturnStatement(new CodeDefaultValueExpression(_property.Type)) },
               new CodeStatement[] { new CodeVariableDeclarationStatement(typeof(string), "tempStr", new CodeCastExpression(new CodeTypeReference(typeof(string)),
                   new CodeVariableReferenceExpression("temp"))),
                   new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(StatusHistory)), "Decode"),
                  new CodeVariableReferenceExpression("tempStr")))
                }));

            classType.Members.Add(_property);

        }
    }
}
